﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LeagueOfRila.Model;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Media;
using Data;



namespace LeagueOfRila.View
{
    public class GameView : BaseView
    {

        Texture2D character;
        Texture2D characterImage;
        public Texture2D map;
        List<Player> othersCharacters;
        public Dictionary<String, Texture2D> othersCharacTexture = new Dictionary<string,Texture2D>();
        Song song;
        public Vector2 mapOrigin = new Vector2(100, 100);
        Texture2D hud;
        public List<Animation> animations = new List<Animation>();
        public Object lockAnim = new Object();
        public bool lockAnimActive = false;
        bool DrawStat = false;

        public GameModel GetModel()
        {
            return this.model as GameModel;
        }
        protected override void LoadContent()
        {
            base.LoadContent();
            this.map = Content.Load<Texture2D>("Maps/mapfinale2");
            this.character = Content.Load<Texture2D>(Client.player.character.sprite);
            //this.graphics.ToggleFullScreen();
            this.GetModel().currentMap.GetSpawnPosition(ref Client.player.posX, ref Client.player.posY, ref this.mapOrigin.X, ref this.mapOrigin.Y, this.GraphicsDevice.Viewport.Width, this.GraphicsDevice.Viewport.Height);
            Animation.textures.Add("sword", Content.Load<Texture2D>("Game/warsword_v2"));
            Animation.textures.Add("circlefire", Content.Load<Texture2D>("Game/circlefire"));
            Animation.textures.Add("boltfire", Content.Load<Texture2D>("Game/circlefire"));
            Animation.textures.Add("confession", Content.Load<Texture2D>("Game/confession"));
            Animation.textures.Add("execution", Content.Load<Texture2D>("Game/circlefire"));
            if (Client.musicLance)
            {
                Client.StopMusic();
            }
            song = Content.Load<Song>("Sounds/Map/mapmusic01");
            Client.mapSongsList.Add(song);
            song = Content.Load<Song>("Sounds/Map/mapmusic02");
            Client.mapSongsList.Add(song);

            this.GetModel().threadUpdate.Start();
        }

        protected override void Draw(GameTime gameTime)
        {
            this.DrawBackgroundColor(3, 64, 65);
            this.DrawImage(map, new Rectangle((int)mapOrigin.X, (int)mapOrigin.Y, map.Width, map.Height));
            this.DrawOtherCharacters();
            this.DrawHUD();
            //this.DrawRectangle(new Rectangle(this.GraphicsDevice.Viewport.Width / 2, this.GraphicsDevice.Viewport.Height / 2, 30, 30), Color.Red);
            this.DrawFont(string.Concat(Client.player.pseudo, " ", (int)mapOrigin.X, " ", (int)mapOrigin.Y), new Vector2(this.GraphicsDevice.Viewport.Width-250, this.GraphicsDevice.Viewport.Height-50), Color.White);
            this.DrawAnimations();
        }

        public void DrawAnimations()
        {
            List<Animation> animationToRemove = new List<Animation>();
            if (!lockAnimActive)
            {
                lock (lockAnim)
                {
                    foreach (Animation anim in animations)
                    {
                        anim.DisplayAnimation(this);
                        if (anim.animationFinished)
                            animationToRemove.Add(anim);
                    }
                    //animations = new List<Animation>();
                    foreach (Animation anim in animationToRemove)
                    {
                        animations.Remove(anim);
                    }

                }
            }
        }
    

        public void DrawOtherCharacters()
        {
            int posx;
            int posy;
            try
            {
               this.GetModel().otherPlayers=  this.GetModel().otherPlayers.OrderBy(x => x.posY).ToList();

                foreach (Player player in this.GetModel().otherPlayers)
                {
                    if (Client.player.pseudo != player.pseudo)
                    {
                        if (this.othersCharacTexture.ContainsKey(player.pseudo))
                        {
                            posx = player.posX + (int)mapOrigin.X;
                            posy = player.posY + (int)mapOrigin.Y;
                            Color? color = null;
                            if (player.character.life == 0)
                            {
                                color = Color.White;
                            }
                            this.DrawImage(this.othersCharacTexture[player.pseudo], new Rectangle(posx, posy, player.character.spriteX, player.character.spriteY), new Rectangle(player.step * player.character.spriteX, player.orientation * player.character.spriteY, player.character.spriteX, player.character.spriteY), color);
                            Vector2 vectorPseudo = new Vector2(player.character.spriteX / 2 - this.font.MeasureString(player.pseudo).X / 2 + posx, posy - 20);
                            this.DrawFont(player.pseudo, vectorPseudo, Color.White);
                        }
                    }
                    else
                    {
                        posx = player.posX + (int)mapOrigin.X;
                        posy = player.posY + (int)mapOrigin.Y;
                        Color? color = null;
                        if (Client.player.character.life == 0)
                        {
                            color = Color.White;
                        }
                        this.DrawImage(this.othersCharacTexture[Client.player.pseudo], new Rectangle(posx, posy, Client.player.character.spriteX, Client.player.character.spriteY), new Rectangle(Client.player.step * Client.player.character.spriteX, Client.player.orientation * Client.player.character.spriteY, Client.player.character.spriteX, Client.player.character.spriteY), color);
                        Vector2 vectorPseudo = new Vector2(Client.player.character.spriteX / 2 - this.font.MeasureString(Client.player.pseudo).X / 2 + posx, posy - 20);
                        this.DrawFont(Client.player.pseudo, vectorPseudo, Color.White);
                    }
                }
            }
            catch (Exception e)
            {
            }
        }

        public void AddTexturePlayer(string pseudo, string sprite)
        {
            Texture2D texturePlayer = Content.Load<Texture2D>(sprite);
             KeyValuePair<string , Texture2D> test = new KeyValuePair<string,Texture2D>(pseudo, texturePlayer);
            if(!this.othersCharacTexture.Contains(test))
                this.othersCharacTexture.Add(pseudo,Content.Load<Texture2D>(sprite));
        }

        public void DrawStats()
        {
            if (DrawStat)
                DrawStat = false;
            else
                DrawStat = true;
        }

        public void DrawHUD()
        {
            //Icone personnage
            characterImage = Content.Load<Texture2D>(Client.player.character.hudicon);
            this.DrawImage(characterImage, new Rectangle(31, 26, 100, 100));

            hud = Content.Load<Texture2D>("Hud/hud");
            this.DrawImage(hud,new Rectangle(10,10,hud.Width, hud.Height));

            //barre de vie
            this.DrawRectangle(new Rectangle(161,38,249,16), Color.DarkRed);
            int currentHp = 249 * Client.player.character.life / Client.player.character.maxLife;
            this.DrawRectangle(new Rectangle(161, 38, currentHp, 16), new Color(255, 57, 23));
            this.DrawRectangle(new Rectangle(161, 40, currentHp, 5), new Color(255, 123, 73));
            //barre de mana
            int currentMana = 0;
            if (Client.player.character.maxMana != 0 )
              currentMana   = 249 * Client.player.character.mana / Client.player.character.maxMana;
       
            this.DrawRectangle(new Rectangle(161, 54, 249, 16), Color.DarkBlue);
            this.DrawRectangle(new Rectangle(161, 54, currentMana, 16), new Color(24, 181, 255));
            this.DrawRectangle(new Rectangle(161, 56, currentMana, 5), new Color(57, 213, 255));

            //texte vie
            string life = string.Concat(Client.player.character.life, " / ", Client.player.character.maxLife);
            Vector2 posLife = new Vector2(161 + 125 - (this.font.MeasureString(life).X) / 2, 35);
            this.DrawFont(life, posLife, Color.White);

            //texte mana
            string mana = string.Concat(Client.player.character.mana, " / ", Client.player.character.maxMana);
            Vector2 posMana = new Vector2(161 + 125 - (this.font.MeasureString(life).X) / 2, 51);
            this.DrawFont(mana, posMana, Color.White);

            //texte nom personnage
            this.DrawFont(String.Concat("Score: ",Client.player.score,"/",Client.player.death), new Vector2(145,75), Color.White);

            if (DrawStat)
            {
                int y = 0;
                this.DrawRectangle(new Rectangle(40,40,720,420), Color.WhiteSmoke);
                foreach (Player player in this.GetModel().otherPlayers)
                {
                    y +=50;
                    if (player.pseudo == Client.player.pseudo)
                        this.DrawFont(Client.player.pseudo + " " + Client.player.score + "/" + Client.player.death, new Vector2(30, y), Color.Black);
                    else
                    this.DrawFont(player.pseudo +" "+player.score+ "/" + player.death, new Vector2(30,y), Color.Black);
                }
               
            }

        }

        
    }
}
